package com.zhufei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import   javax.servlet.jsp.PageContext;

import com.zhufei.dao.Paging;
import com.zhufei.dao.SponsorDao;
public class donatorServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public donatorServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		System.out.println("action is:"+action);

		if (action == null) {

		}

		if (action.equals("add")) {
			this.add(request, response);
		}
		if(action.equals("manage")){
			this.manage(request, response);
		}
		if(action.equals("edit")){
			this.edit(request, response);
		}
		if(action.equals("delete"))
		{
			this.delete(request, response);
		}
		if(action.equals("more"))
		{
			this.showAll(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	     String donatorFirmName = request.getParameter("donatorFirmName");
	     String handicappedName = request.getParameter("handicappedName");
	     String time  = request.getParameter("time");
	     String sponsorType =request.getParameter("sponsor");
	     int type = 0;
		 System.out.println("SponorType :"+sponsorType);
		 if(sponsorType.equals("qiye"))
		 {
			 type = 1;
		 }
	     boolean addReasult = SponsorDao.insertSponsor(donatorFirmName,0,time,handicappedName,type);
	     if(addReasult)
	     {
	    	 System.out.println("sucesfully operator");
	    	 this.manage(request, response);
	     }
	     else
	     {
	     }
	}
	private void edit(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String donatorfirmName = request.getParameter("donator");
		String handicappedName = request.getParameter("handicapped");
		String time = request.getParameter("time");
		String id = request.getParameter("id");
		 String sponsorType =request.getParameter("sponsor");
	     int type = 0;
		 System.out.println("SponorType :"+sponsorType);
		 if(sponsorType.equals("qiye"))
		 {
			 type = 1;
		 }
		boolean editResult = SponsorDao.updateSponsor(id, donatorfirmName, 0,time, handicappedName,type);
		if(editResult)
	     {
			this.manage(request, response);
	       }
	     else
	     {
	    	 
	     }
		
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String sponsorId = request.getParameter("id");
		
		boolean isSucc = SponsorDao.deleteSponsor(sponsorId);
		if(isSucc){
			this.manage(request, response);
		}else{
			
		}
	}
	private void manage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		SponsorDao donators = new SponsorDao();
		donators.getConnection();
		int donatorCount =donators.getCountOfSponsors();
		ArrayList donatorsList = donators.getSponsors(0, donatorCount);
		HttpSession session = request.getSession();
		session.setAttribute("sponsorList", donatorsList);
		Paging paging = new Paging();
		//取得page值
		String strPage = paging.strPage(request, "page");
		int current_Page = paging.current_Pages(strPage);
		//设置一页显示多少行数据
		int page_record = paging.setPage_record(10);
		//取得总页数
		int total_page = paging.getTotal_Pages(donatorCount);
		//保存当前页数
		session.setAttribute("current_page", current_Page);
		//保存总页数
		session.setAttribute("total_page", total_page);
		session.setAttribute("page_record", page_record);
		response.sendRedirect("admin/manageDonator.jsp");
		
	}
	public void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException 
	{
		System.out.println("call showall");
		SponsorDao donators = new SponsorDao();
		donators.getConnection();
		int donatorCount =donators.getCountOfSponsors();
		ArrayList donatorsList = donators.getSponsors(0, donatorCount);
		HttpSession session = request.getSession();
		session.setAttribute("donatorList", donatorsList);
		Paging paging = new Paging();
		//取得page值
		String strPage = paging.strPage(request, "page");
		int current_Page = paging.current_Pages(strPage);
		//设置一页显示多少行数据
		int page_record = paging.setPage_record(40);
		//取得总页数
		int total_page = paging.getTotal_Pages(donatorCount);
		//保存当前页数
		session.setAttribute("current_page_all", current_Page);
		//保存总页数
		session.setAttribute("total_page_all", total_page);
		session.setAttribute("page_record_all", page_record);
		//response.sendRedirect("../moreloves.jsp");
		PrintWriter out = response.getWriter();
		response.sendRedirect("www.baidu.com");
		//out.print("<script language='javascript'>window.location.href='../moreloves.jsp'</script>");
		
	}
	public void init() throws ServletException {
		// Put your code here
	}

}
